<?php

namespace App\User\Models;

use App\BaseModel;
use App\Medical\Models\Rescue;
use Illuminate\Database\Eloquent\Model;

class Archive extends BaseModel
{
    protected static $area_json;

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $file = config('his.area_file');
        if(is_file($file) && self::$attribute_switch){
            self::$area_json = file_get_contents(config('his.area_file'));
        }
    }

    protected $table = 'zwj_his_user_archives';

    protected $guarded = [];

    const PROFESSION_TYPE = [
        'COUNTRY' => 1,
        'SCIENCE' => 2,
        'WORK' => 3,
        'BUSINESS' => 4,
        'FARMER' => 5,
        'PRODUCT' => 6,
        'SOLDIER' => 7,
        'OTHER' => 8
    ];

    const PROFESSION_TYPE_DESC = [
        1 => '公务人员',
        2 => '专业技术人员',
        3 => '办事人员和有关人员',
        4 => '商业、服务业人员',
        5 => '农、林、牧、渔、水利业生产人员',
        6 => '生产、运输设备操作人员及有关人员',
        7 => '军人',
        8 => '其他人员'
    ];

    const CARD_TYPE = [
        'ID_CARD' => 1
    ];

    const CARD_TYPE_DESC = [
        1 => '居民身份证'
    ];

    protected $appends = [
        'profession_type_text', 'address_name', 'card_type_name'
    ];

    /**
     * User: Virus
     * DateTime: 2021/6/22 9:05
     * Describe: 获取职业名称
     * @param $table
     * @return string
     */
    public function getProfessionTypeTextAttribute($table)
    {
        if($this->profession_type){
            return self::PROFESSION_TYPE_DESC[$this->profession_type];
        }
        return '';
    }

    /**
     * User: Virus
     * DateTime: 2021/6/22 10:36
     * Describe: 获取地址
     * @return string
     */
    public function getAddressNameAttribute()
    {
        if(!self::$area_json){
            return '';
        }

        $preg = '/\"label\"\:\"([\x{4e00}-\x{9fa5}]+?)\",\"value\"\:|num|/u';
        $name = '';
        switch (true){
            case !empty($this->address_province):
                preg_match(str_replace('|num|', $this->address_province, $preg), self::$area_json, $matches);
                $name .= $matches[1].',';
            case !empty($this->address_city):
                preg_match(str_replace('|num|', $this->address_city, $preg), self::$area_json, $matches);
                $name .= $matches[1].',';
            case !empty($this->address_county):
                preg_match(str_replace('|num|', $this->address_county, $preg), self::$area_json, $matches);
                $name .= $matches[1].',';
        }
        return rtrim($name, ',');
    }

    /**
     * User: Virus
     * DateTime: 2021/6/22 11:23
     * Describe: 证件类型描述
     * Version:
     */
    public function getCardTypeNameAttribute()
    {
        if(!empty($this->card_type)){
            return self::CARD_TYPE_DESC[$this->card_type];
        }
    }

    public function hasRescue()
    {
        return $this->hasMany(Rescue::class, 'archive_id');
    }


}
